//
//  PopoverDelegate.swift
//  WeiBo
//
//  Created by Tony on 2018/1/3.
//  Copyright © 2018年 Tony. All rights reserved.
//

import UIKit

class PopoverDelegate: NSObject {

    var isPopover = false
    
    // MARK: -对外属性
    public var presentedViewFrame: CGRect = CGRect.zero
    
    public var callBack: ((_ isPopover: Bool)->())?
    
    override init() {
        
    }
    init(callBack: ((_ isPopover: Bool)->())?) {
        self.callBack = callBack
    }
    
}

// MARK: -UIViewControllerTransitioningDelegate
extension PopoverDelegate: UIViewControllerTransitioningDelegate {
    
    /// 目的：重设弹出view.frame
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let vc = PopoverPresentationController.init(presentedViewController: presented, presenting: presenting)
        vc.presentedViewFrame = presentedViewFrame
        
        return vc
    }
    
    /// 目的：自定义弹出动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        // UIViewControllerAnimatedTransitioning是个协议，点击进入文档查看
        // UIViewControllerAnimatedTransitioning？就是需要返回一个遵守协议的对象
        
        isPopover = true
        
        if callBack != nil {
            callBack!(true)
        }
        
        return self
    }
    /// 目的：自定义消失动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPopover = false
        
        if callBack != nil {
            callBack!(false)
        }
        
        return self
    }
}
// MARK: -UIViewControllerAnimatedTransitioning
extension PopoverDelegate: UIViewControllerAnimatedTransitioning {
    
    /// 动画时长
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.25
    }
    
    /// 取到 “上下文”，通过上下文自定义弹出与消失动画
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        // Currently only two keys are defined by the system -
        // UITransitionContextFromViewKey, and UITransitionContextToViewKey
        // viewForKey: may return nil which would indicate that the animator should not
        // manipulate the associated view controller's view.
        
        isPopover ? presentedAnim(context: transitionContext) : dismissAnim(context: transitionContext)
        
    }
    /// 自定义弹出动画
    private func presentedAnim(context transitionContext: UIViewControllerContextTransitioning) {
        // 通过上下文取弹出view
        let presentedView = transitionContext.view(forKey: .to)!
        
        // 将presentedView添加到containerView中
        transitionContext.containerView.addSubview(presentedView)
        
        // 设置形变
        presentedView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0001)
        
        // 设置锚点
        presentedView.layer.anchorPoint = CGPoint.init(x: 0.5, y: 0)
        
        // 执行动画
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
            
            // presentedView.transform = CGAffineTransform.init(scaleX: 1.0, y: 1.0)
            
            // 清空形变
            presentedView.transform = CGAffineTransform.identity
            
        }) { [weak self] (_) in
            // 正式告诉转场上下文，已结束动画
            transitionContext.completeTransition(true)
            
            self?.isPopover = true
        }
    }
    
    /// 自定义消失动画
    private func dismissAnim(context transitionContext: UIViewControllerContextTransitioning) {
        
        // 通过上下文取消失view
        let dissmissView = transitionContext.view(forKey: .from)!
        
        // 设置锚点
        dissmissView.layer.anchorPoint = CGPoint.init(x: 0.5, y: 0)
        
        // 执行动画
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
            
            // 清空形变
            // IOS对0这样的边界支持不好，所以写0.00001
            dissmissView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.00001)
            
        }) { [weak self] (_) in
            // 正式告诉转场上下文，已结束动画
            transitionContext.completeTransition(true)
            
            self?.isPopover = false
        }
    }
}
